home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / tarz < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  5.7 KB  |  193 lines

  1. #!/bin/ksh
  2. # @(#) tarz.ksh 2.0 96/06/06
  3. # 92/10/09 john h. dubois iii (john@armory.com)
  4. # 93/04/15 Added H option.  Fixed f option.
  5. # 94/04/04 Added q option.
  6. # 95/07/02 Pass . to tar instead of expanding *
  7. # 96/01/15 Allow use of various compression utilities, read rcfile,
  8. #          added t option, pass assorted other options to compressor.
  9. # 96/01/21 Use rcfile in UHOME also.
  10. # 96/06/06 Ignore chars in $COPTS that do not apply to the compressor in use.
  11. #          Added d option.
  12.  
  13. name=${0##*/}
  14. cOpts=H0123456789
  15. Usage="Usage: $name [-cdfghpqvt] [-$cOpts] dirname ..."
  16. test=false
  17. Debug=false    # not currently used for anything
  18. compress=compress
  19. set -A VFlags -- -v -v
  20. set -A Comps -- compress gzip pack
  21. set -A Exts -- .Z .gz .z
  22. set -A oFlags -- H 0123456789    # optional flags
  23. typeset -i nComp=${#Comps[*]}
  24. rcFile=.tarzrc
  25.  
  26. COPTS=
  27. QUIET=0
  28. FORCE=0
  29. COMP=compress
  30. # Source these before processing opts so assignments can be overridden.
  31. [ -f "$UHOME/$rcFile" -a -r "$UHOME/$rcFile" ] && . "$UHOME/$rcFile"
  32. [ -f "$HOME/$rcFile" -a -r "$HOME/$rcFile" ] && . "$HOME/$rcFile"
  33.  
  34. while getopts :cdfhvpqgtx$cOpts opt; do
  35.     case $opt in
  36.     h) echo \
  37. "$name: archive a directory.
  38. $Usage
  39. The contents of each dirname are tarred and compressed into an archive in the
  40. current directory with the name dirname.tar.Z.  Pathnames in the archives
  41. are relative to dirname.  
  42. Some options can be set in a configuration file named \"$rcFile\", which may
  43. be in the invoking user's home directory or in the directory specified by the
  44. environment variable UHOME (if both exist, assignments in the former take
  45. precedence).  In this file, values are assigned to variables in shell style,
  46. e.g. \"varname=value\".  Options given in the configuration files are
  47. overridden by those given on the command line.  Variable names are given in
  48. parentheses following option descriptions.
  49. Options:
  50. -h: Print this help.
  51. -p: Use 'pack' to compress.   In the configuration file, put: COMP=pack
  52. -g: Use 'gzip' to compress.   In the configuration file, put: COMP=gzip
  53. -c: Use 'compress' to compress.   This is the default.
  54. -n: Check for dirname.tar and compressed dirname.tar before writing output,
  55.     and abort if either exists.  This is the default.
  56. -f: Force writing of output even if dirname.tar or compressed dirname.tar
  57.     exists.  In the configuration file, put: FORCE=1
  58. -q: Be quiet; do not verbose flag to compressor or list output file.  In the
  59.     configuration file, put: QUIET=1
  60. -t: Tell what command would be run, but do not execute it.
  61. -v: Be verbose; give verbose flag to compressor (if it has one), and list
  62.     output file after archiving is completed.  This is the default.
  63. To set any of the following options in the configuration file, use:
  64. COPTS=options.  If any of these options are given on the command line, those in
  65. the configuration file are ignored.  If COPTS is used in the configuration
  66. file, any options that do not apply to the compressor in use are ignored.
  67. -d: Dummy option to disable use of any value assigned to COPTS in config file.
  68. -H: Make 'compress' do better compression.
  69. -[0-9]: Set the compression level for 'gzip'."
  70.     exit 0
  71.     ;;
  72.     f)  FORCE=1;;
  73.     n)  FORCE=0;;
  74.     c)  COMP=compress;;
  75.     d)  COPTS=;;
  76.     g)  COMP=gzip;;
  77.     p)  COMP=pack;;
  78.     q)    QUIET=1;;
  79.     v)    QUIET=0;;
  80.     t)    test=true;;
  81.     x)  Debug=true;;
  82.     [$cOpts]) cOptions="$cOptions$opt";;
  83.     +?)
  84.     print -u2 "$name: options should not be preceded by a '+'."
  85.     exit 1
  86.     ;;
  87.     :) 
  88.     print -r -u2 -- \
  89.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  90.     exit 1
  91.     ;;
  92.     ?) 
  93.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  94.     exit 1
  95.     ;;
  96.     esac
  97. done
  98.  
  99. [ "$QUIET" = 0 ] && Quiet=false || Quiet=true
  100. [ "$FORCE" = 0 ] && Check=true || Check=false
  101.  
  102. typeset -i i=0
  103. while [ i -lt nComp ]; do
  104.     [ "$COMP" = "${Comps[i]}" ] && break
  105.     let i+=1
  106. done
  107. if [ i -ge nComp ]; then
  108.     print -u2 "Unknown compression type: $COMP.  Aborting."
  109.     exit 1
  110. fi
  111. vflag=${VFlags[i]}
  112. Ext=${Exts[i]}
  113. $Quiet && vflag= || vflag=${VFlags[i]}
  114.  
  115. # Usage: SplitStr <string>
  116. # Each character in <string> is put in a separate element in SplitStr_ret[],
  117. # starting with 0.
  118. # If <string> has more characters than the maximum number of elements in an
  119. # array (1024), 1 is returned.  Otherwise 0 is returned.
  120. function SplitStr {
  121.     typeset s=$1
  122.     typeset -i i=0
  123.     unset SplitStr_ret[*]
  124.     [ ${#s} -gt 1024 ] && return 1
  125.     while [ -n "$s" ]; do
  126.     ns=${s#?}
  127.     SplitStr_ret[i]=${s%$ns}
  128.     s=$ns
  129.     let i+=1
  130.     done
  131.     return 0
  132. }
  133.  
  134. # Usage: commonStr s1 s2
  135. # Sets commonStr_ret to a string consisting of s1 with all of its characters
  136. # that are not in s2 removed.
  137. function commonStr {
  138.     typeset c
  139.  
  140.     SplitStr "$1"
  141.     commonStr_ret=
  142.     for c in "${SplitStr_ret[@]}"; do
  143.     # Use case instead of [[]] to avoid need to eval pattern comparison
  144.     case "$c" in
  145.     [$2]) commonStr_ret="$commonStr_ret$c"
  146.     esac
  147.     done
  148. }
  149.  
  150. oFlagList=${oFlags[i]}
  151. if [ -z "$cOptions" ]; then
  152.     commonStr "$COPTS" "$oFlagList"
  153.     cOptions=$commonStr_ret
  154. fi
  155. [ -n "$cOptions" ] && cOptions="-$cOptions"
  156.  
  157. # remove args that were options
  158. let OPTIND=OPTIND-1
  159. shift $OPTIND
  160.  
  161. if [ $# -lt 1 ]; then
  162.     print -u2 "$Usage\nUse -h for help."
  163.     exit
  164. fi
  165.  
  166. OWD=$PWD
  167.  
  168. for dirname; do
  169.     cd -- "$OWD"
  170.     archname="${dirname##*/}.tar"
  171.     if $Check; then
  172.     if [ -f "$archname" ]; then
  173.         print -u2 -- \
  174.     "$archname: file exists.  Use the -f option to overwrite.  Skipping."
  175.         continue
  176.     fi
  177.     if [ -f "$archname$Ext" ]; then
  178.         print -u2 -- "$archname$Ext: file exists.  Skipping."
  179.         continue
  180.     fi
  181.     fi
  182.     if cd -- "$dirname"; then :; else
  183.     print -u2 -- "Could not cd to '$dirname'.  Not processed."
  184.     continue
  185.     fi
  186.     if $test; then
  187.     print -- "tar cf - . | $COMP $vflag $cOptions > $OWD/$archname$Ext"
  188.     else
  189.     tar cf - . | $COMP $vflag $cOptions > "$OWD/$archname$Ext"
  190.     $Quiet || l -- "$OWD/$archname$Ext"
  191.     fi
  192. done
  193.